local app = require "app"
local game = require "club.threecard.hbsl"
local const = game.util.const

local M = game.ui.element()

M.store_watchers = {
    gameplay = {
        ["$"] = function(self, store, state, args)
        end,

        ["红包现世"] = function(self, store, state, args)
            local player = args.player
            local amount = args.amount
            local count = args.count
            local no = args.no

            self:appear(player, amount, count, no)
        end,

        ["红包变更"] = function(self, store, state, args)
            self.count_txt.text = state.left_count
            if not args then
                return
            end
            if state.left_count <= 0 then
                local player = args.player
                local no = args.no
                self:reveal(player, no)
            end
        end,

        ["红包结果"] = function(self, store, state, args)
            local player = args.player
            local no = args.no
            local win = args.win

            self:reveal(player, no, win)
        end,

        ["状态变更"] = function(self, stroe, state, args)
            if state.phase_id == const.PHASE_IDLE then
                self:hide()
            end
        end
    }
}

function M:ctor()
    self.res = "club.threecard.hbsl/ui/ingame.b:hb_shower"

    local AvatarIcon = require "ui.Common.AvatarIcon"
    self.avatar = AvatarIcon.new()
    self.result_avatar = AvatarIcon.new()
end

function M:on_load()
    self:super("on_load")

    self.avatar:load()
    self.avatar:set_parent(self.avatar_root, false)

    self.result_avatar:load()
    self.result_avatar:set_parent(self.result_avatar_root, false)
end

function M:on_injected(bind)
    bind("button.click", self.btn_click, self.click_ivk)
end

function M:click_ivk()
    game.audio.post("hbsl:click")
    game.store.gameplay.grab_packet()
end

function M:appear(player, amount, count, no)
    local i = math.random(1, 10)
    if i < 2 then
        game.audio.post("hbsl:send_random_1")
    elseif i < 4 then
        game.audio.post("hbsl:send_random_2")
    elseif i< 6 then
        game.audio.post("hbsl:send_random_3")
    elseif i< 8 then
        game.audio.post("hbsl:send_random_4")
    elseif i< 10 then
        game.audio.post("hbsl:send_random_5")
    end

    self:show()
    self.on_grab_canvas.enabled = true
    self.on_reveal_canvas.enabled = false
    self.amount_txt.text = app.i18n.number(amount, "chip3")
    self.count_txt.text = count
    self.no_txt.text = no
    self.avatar:set_head(player.head)
    self.avatar:set_flag(player.country)
end

function M:reveal(player, no, win)
    self:show()
    self.on_grab_canvas.enabled = false
    self.on_reveal_canvas.enabled = true

    if player then
        self.result_avatar:set_head(player.head)
        self.result_avatar:set_flag(player.country)
    end

    if win then
        if win >= 0 then
            self.result_txt.text = "+" .. app.i18n.number(win, "chip4")
        else
            self.result_txt.text = "-" .. app.i18n.number(-win, "chip4")
        end
    else
        self.result_txt.text = ""
    end

    self.result_no_txt.text = no
end

return M